<?php

class m120216_205103_create_base_structure extends CDbMigration
{
	public function up()
	{
		$this->createTable('user', array(
			'id'=>'pk',
			'first_name'=>'string NOT NULL',
			'last_name'=>'string NOT NULL',
			'email'=>'string NOT NULL',
			'password'=>'string NOT NULL',
			'image_url'=>'string',
			'content'=>'text',
		));
		
		$this->createTable('store', array(
			'id'=>'pk',
			'name'=>'string NOT NULL',
			'logo_url'=>'string NOT NULL',
			'content'=>'text',
			'chat_code'=>'text',
			'visit_price'=>'decimal(10,2)',
			'date'=>'datetime',
			'visits_number'=>'integer',
			'new_visit_price'=>'decimal(10,2)',
			'balance'=>'decimal(10,2)',
			'user_id'=>'integer',
			'product_id'=>'integer',
		));
		
		$this->createTable('type', array(
			'id'=>'pk',
			'name'=>'string NOT NULL',
			'content'=>'text',
		));
		$this->insert('type', array('name'=>'Laptops'));
		$this->insert('type', array('name'=>'Computadoras'));
		$this->insert('type', array('name'=>'Tablets'));
		$this->insert('type', array('name'=>'Impresoras'));
		$this->insert('type', array('name'=>'Accesorios'));
		
		$this->createTable('product', array(
			'id'=>'pk',
			'name'=>'string NOT NULL',
			'price_from'=>'string',
			'content'=>'text',
			'store_id'=>'integer',
			'type_id'=>'integer',
			'media_id'=>'integer',
			'visible'=>'boolean',
		));
		
		$this->createTable('fee', array(
			'id'=>'pk',
			'name'=>'string NOT NULL',
			'code'=>'string',
			'date_time'=>'datetime',
			'amount'=>'decimal(10,2)',
			'content'=>'text',
			'store_id'=>'integer',
			'visible'=>'boolean',
		));
		
		$this->createTable('account', array(
			'id'=>'pk',
			'date'=>'datetime',
			'visits_number'=>'integer',
			'visit_price'=>'decimal(10,2)',
			'fee_amount'=>'decimal(10,2)',
			'balance'=>'decimal(10,2)',
			'store_id'=>'integer',
		));
		
		$this->createTable('media', array(
			'id'=>'pk',
			'name'=>'string',
			'url_400'=>'string NOT NULL',
			'url_110'=>'string NOT NULL',
			'url_80'=>'string NOT NULL',
			'content'=>'text',
			'product_id'=>'integer',
		));
		
		$this->createTable('config', array(
			'id'=>'pk',
			'name'=>'string NOT NULL',
			'value'=>'string NOT NULL',
		));
		
		$this->addForeignKey('product_store', 'product', 'store_id', 'store', 'id');
		$this->addForeignKey('fee_store', 'fee', 'store_id', 'store', 'id');
		$this->addForeignKey('account_store', 'account', 'store_id', 'store', 'id');
		$this->addForeignKey('product_type', 'product', 'type_id', 'type', 'id');
		$this->addForeignKey('media_product', 'media', 'product_id', 'product', 'id');
		$this->addForeignKey('store_user', 'store', 'user_id', 'user', 'id');
	}

	public function down()
	{	
		$this->dropTable('config');
		$this->dropTable('media');
		$this->dropTable('product');
		$this->dropTable('fee');
		$this->dropTable('account');
		$this->dropTable('store');
		$this->dropTable('type');
		$this->dropTable('user');
	}

	/*
	// Use safeUp/safeDown to do migration with transaction
	public function safeUp()
	{
	}

	public function safeDown()
	{
	}
	*/
}